<?php 

class suipaiModel extends appModel
{
	
//  是否已点赞
	public function check_is_praise($wxid,$pid)
	{
		$sql = "select count(*) from ".$this->table('praise')." where wxid='".$wxid."' and pid='".$pid."'";
		return $this->getOne($sql);
	}
	
// 	是否已提交作品
	public function check_is_picture($wxid)
	{
		$sql = "select count(*) from ".$this->table('picture')." where wxid='".$wxid."'";
		return $this->getOne($sql);
	}
	
// 	新增记录
	public function insert_record($info,$table)
	{
		$data = yf_set_insert($info);
		$sql = "insert into ".$this->table($table)." (".$data['key'].") values (".$data['val'].")";
		$this->query($sql);
		return $this->insert_id();
	}
	
	//点赞
	public function insert_praise($info)
	{
		$back = $this->insert_record($info, "praise");
		
		
		if($back)
		{
			$this->setInc("praise", 1, "picture", $info['pid']);
		}
		
		return $back;
	}
	

// 	字段自增
	public function setInc($field,$step,$table,$id)
	{
		
		
		$sql = " update ".$this->table($table)." set ".$field." = ".$field." + ".$step." where id = '".$id."'";
	
		return $this->query($sql);
	}
	
// 	作品数
	public function getCount()
	{
		
		$sql = "SELECT count(*) FROM ".$this->table('picture')."  WHERE  status = 1 ";
		$result = $this->getOne($sql);
		
	
		return $result;
	}
	
// 	获取作品 默认排序
	public function get_list()
	{
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('picture')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.id WHERE p1.status =1  ORDER BY created ";
	
		$result = $this->getAll($sql);
		
		
		
		foreach ($result as $k => $v)
		{
			
			$v['pic'] = "http://".$_SERVER['HTTP_HOST']."/ws/".$v['pic'];
			
			$result[$k]= $v;
		}
		
		
		
	
		return $result;
	}
	
	
	public function get_match()
	{
	    $sql = "SELECT p1.* FROM ".$this->table('picture')." as p1 ".
	         " WHERE p1.id  ='".$_SESSION['wxid']."'";
	
	    $result = $this->getRow($sql);

	    return $result;
	}
	
	public function get_parent($wxid)
	{
	    $sql = "SELECT p1.* FROM ".$this->table('picture')." as p1 ".
	        " WHERE p1.id  ='".$wxid."'";
	
	    $result = $this->getRow($sql);
	
	    return $result;
	}
	
// 	获取作品 点赞排序
	public function get_list_parise()
	{
		$sql = "SELECT p1.*,p2.nickname,p2.openid FROM ".$this->table('picture')." as p1 ".
				"LEFT JOIN ".$this->table('wxusers')." as p2 on p1.wxid = p2.id WHERE p1.status =1  ORDER BY praise desc ,created ";
	
		$result = $this->getAll($sql);
	
	
	
		foreach ($result as $k => $v)
		{
				
			$v['pic'] = "http://".$_SERVER['HTTP_HOST']."/ws/".$v['pic'];
				
			$result[$k]= $v;
		}
	
	
	
	
		return $result;
	}
	
	
	
}

?>